package com.xdjj.market.web.controller;

import com.xdjj.market.pojo.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {
    @PostMapping("/user")
    public String add(@RequestBody User user){
        System.out.println(user);
        return "保存成功！";
    }

    @PutMapping("/user/{id}")
    public String update(@RequestBody User user, Integer id){
        System.out.println(user);
        return "修改成功！";
    }

    @DeleteMapping("/user/{id}")
    public String remove(Integer id){
        System.out.println(id);
        return "删除成功！";
    }

    @GetMapping("/user")
    public List<User> list(){
        List<User> userList = new ArrayList<>();
        //添加元素---自己模拟两个用户
        User user1 =new User();
        user1.setId(1);
        user1.setUserName("Rose");
        user1.setAge(18);

        User user2 =new User();
        user2.setId(2);
        user2.setUserName("Jack");
        user2.setAge(88);

        //将用户添加到列表
        userList.add(user1);
        userList.add(user2);

        //返回列表
        return userList;
    }

    //路径参数示例，@PathVariable关键字，访问/user/123
    @GetMapping("/user/{id}")
    public User getById(@PathVariable Integer id){
        System.out.println(id);

        User user1 =new User();
        user1.setId(id);
        user1.setUserName("Rose");
        user1.setAge(18);

        return user1;
    }

    //查询参数示例，访问/user/query?id=123
    @GetMapping("/user/query")
    public User getById2(Integer id){
        System.out.println(id);

        User user1 =new User();
        user1.setId(id);
        user1.setUserName("Rose");
        user1.setAge(18);

        return user1;
    }
}
